<template>
  <div class="departments-container">
    <el-card>
      <el-tabs v-model="activeName">
        <el-tab-pane label="组织结构" name="first" />
      </el-tabs>

      <div class="box">
        <!-- 标题 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end" style="text-align: right">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="addDep('')"
                    >添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 树形组件列表 -->
        <el-tree :data="data" :props="defaultProps">
          <template slot-scope="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end" style="text-align: right">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="addDep(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="ediDep(data)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </div>
    </el-card>

    <!-- 添加部门对话框 -->
    <el-dialog title="提示" :visible.sync="dialogVisibleAdd" width="50%">
      <deptDialog
        v-if="dialogVisibleAdd"
        ref="dept"
        :visible.sync="dialogVisibleAdd"
        :pid="pid"
        :is-edit="isEdit"
        :row="row"
        @success="success"
      />
    </el-dialog>
    <!-- 添加部门对话框 -->
  </div>
</template>
<script>
import { getDepartments, delDepartment } from '@/api/departments'
import arrayToTree from 'array-to-tree'
import deptDialog from './deptDialog.vue'
export default {
  components: {
    deptDialog
  },
  data() {
    return {
      activeName: 'first',
      data: [
        {
          label: '一级 1',
          children: [
            {
              label: '二级 1-1',
              children: [
                {
                  label: '三级 1-1-1'
                }
              ]
            }
          ]
        },
        {
          label: '一级 2',
          children: [
            {
              label: '二级 2-1',
              children: [
                {
                  label: '三级 2-1-1'
                }
              ]
            },
            {
              label: '二级 2-2',
              children: [
                {
                  label: '三级 2-2-1'
                }
              ]
            }
          ]
        },
        {
          label: '一级 3',
          children: [
            {
              label: '二级 3-1',
              children: [
                {
                  label: '三级 3-1-1'
                }
              ]
            },
            {
              label: '二级 3-2',
              children: [
                {
                  label: '三级 3-2-1'
                }
              ]
            }
          ]
        }
      ],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      /* 添加部门 */
      dialogVisibleAdd: false,
      pid: '',
      // 判断是编辑框还是添加框
      isEdit: true,
      row: {}
    }
  },
  created() {
    this.getDep()
  },
  methods: {
    //  获取简单员工列表
    async getDep() {
      const res = await getDepartments()
      this.data = arrayToTree(res.data.depts, {
        parentProperty: 'pid',
        customID: 'id'
      })
    },
    /* 添加部门 */
    addDep(pid) {
      this.dialogVisibleAdd = true
      this.pid = pid
      this.isEdit = false
    },
    success() {
      this.dialogVisibleAdd = false
      // 刷新数据
      this.getDep()
    },
    // 删除部门
    async hDel(id) {
      const res = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)
      console.log(`res`, res)
      if (res === 'confirm') {
        // 说明我点击的是确认按钮
        const res = await delDepartment(id)
        console.log(res, 333)
        if (res.code === 10000) {
          this.getDep() // 刷新页面
        }
      }
    },
    // 编辑部门
    ediDep(row) {
      this.dialogVisibleAdd = true
      this.isEdit = true
      this.row = row
    },
    /* 监听对话框关闭函数 */
    closeDia() {
      console.log(this.$refs.dept, 222222)
      //  this.$refs.dept====获取子组件的this
      this.$refs.dept.close()
    }
  }
}
</script>
<style lang="scss" scoped>
.departments-container {
  padding: 30px;
  .box {
    padding: 30px 50px;
  }
}
</style>
